package java8.lambda;

import java.util.*;

/**
 * Created by CharloYao on 2017/3/15.
 * Lambda基本语法：([parameter…]) -> { statements; } //[]表可选，即括号可以无参
 */
public class Client {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");

        //传统方式的实现
        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return b.compareTo(a);
            }
        });

        //Lambda表达式方法
        Collections.sort(names,(String a,String b )->{
            return b.compareTo(a);
        });

        Collections.sort(names,(a,b) ->{
            return b.compareTo(a);
        });

        Collections.sort(names,(a,b) -> b.compareTo(a));

        //List中定义了forEach方法，参数为Lambda表达式
        names.forEach(str -> names.remove(str));

        //其中的::符号之前是作用域，之后是具体方法，例如Objest::hashCode，上图中也一样System.out其实是一个PrintStream对象，PrintStream对象有一个Println方法。
        names.forEach(System.out::println);
    }

    private static void testRunnable(){
        //普通方法
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println(1);
            }
        };

        //Lambda表达式
        Runnable runnable1 = ()->{
            System.out.println(2);
        };
        Thread thread = new Thread(runnable1);

        //也可以这样写
        Thread thread1 = new Thread(()->{
            System.out.println(1);
        });
    }
}
